/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.app.context.abstract.FeverContext;
import fever.app.context.abstract.IContextContainer;
import fever.app.context.abstract.IDialog;
import fever.app.context.abstract.IFileSystem;
import fever.app.context.browser.BrowserContext;
import fever.log.FeverDebug;

/**
 * Defines context where application is running.
 * 
 * <p>Context is used to define some basic properties for application container.
 * <ul>
 *   <li>The container ( window ) caption</li>
 *   <li>Container position</li>
 *   <li>Container size</li>
 * </ul>
 * 
 * <p>Use {@link fever.Fever#run()} method to define context at startup.<br />
 * Example : 
 * {@code 
 *   Fever.run( new MyMainClass(), BrowserContext.getInstance() );
 *  }
 * 
 * <p>If no factory is defined, use default 
 * {@link fever.app.context.browser.BrowserContext}
 * 
 * @see fever.app.context.abstract.ContextContainer
 * @see fever.app.context.abstract.FeverContext
 * 
 * @author Romain Ecarnot
 */
class fever.app.context.ContextManager
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : ContextManager;
	
	private var _context : FeverContext;
	private var _container : IContextContainer;	private var _io : IFileSystem;	private var _dialog : IDialog;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
		
	/** Read only. Returns concrete container with current context. */
	public function get container() : IContextContainer { return _container; }
	
	/** Read only. Returns concrete context. */	public function get context() : FeverContext { return _context; }
	
	/** Read only. Returns concrete filesystem. */
	public function get io() : IFileSystem { return _io; }
	
	/** Read only. Returns concrete dialog system. */
	public function get dialog() : IDialog { return _dialog; }
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	/**
	 * Defines context for application.
	 * 
	 * <p>Default use {@link fever.app.context.browser.BrowserContext} context 
	 * 
	 * @param factory {@link fever.app.context.abstract.FeverContext} subclass
	 */
	public static function getInstance( factory : FeverContext ) : ContextManager
	{
		if( !_instance ) _instance = new ContextManager( factory );
		return _instance;
	}
	
	/**
	 * Returns current <strong>Fever</strong> context.
	 */
	public function getName() : String
	{
		return _context.toString();	
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function ContextManager( factory : FeverContext )
	{
		if( !factory ) 
		{
			FeverDebug.WARN( 'use default browser context' );
			factory = BrowserContext.getInstance();
		}
		
		_context = factory;
		_container = factory.createContainer();	
		_io = factory.createFileSystem();		_dialog = factory.createDialog();
		
		FeverDebug.INFO( 'Context : ' + factory );
	}	
}